package com.party.tool;

import java.io.*;  
import java.util.*;  
import java.net.*;  
public class HttpTookit {  
//发送http的get请求  
public static String doGet(String url){  
String result="";  
BufferedReader in=null;  
try{  
String urlstring=url;
URL realurl=new URL(urlstring);  
System.out.println("请求的服务器主机域名："+realurl.getHost().toString());  
//打开与此URL的连接  
URLConnection connection=realurl.openConnection();  
//设置请求连接时间和读取数据时间  
connection.setConnectTimeout(3000);  
connection.setReadTimeout(7000);  
//建立实际的连接  
connection.connect();  
//读取获取的数据  
in=new BufferedReader(new InputStreamReader(connection.getInputStream()));  
String line;  
while((line=in.readLine())!=null){  
result+=line;  
}   
}catch(Exception e){  
System.out.println("发送GET请求出现异常！" + e);  
e.printStackTrace();  
}finally {  
try{  
if(in!=null){  
in.close();  
}  
}catch (Exception e2) {  
System.out.println("关闭请求流出现异常！" + e2);  
e2.printStackTrace();  
}  
}  
return result;  
}  
//发送http的post请求  
public static String dopost(String url,String param){  
String result="";  
BufferedReader in=null;  
PrintWriter out=null;  
try{  
String urlstring=url;  
URL realurl=new URL(url);  
System.out.println("请求的服务器主机域名："+realurl.getHost().toString());  
//打开与此URL的连接  
URLConnection connection=realurl.openConnection();  
//设置请求连接时间和读取数据时间  
connection.setConnectTimeout(3000);  
connection.setReadTimeout(7000);  
//post请求的时候必须要设置的两个属性  
connection.setDoOutput(true);  
connection.setDoInput(true);  
//获取URLconnextion对象对应的输出流  
out=new PrintWriter(connection.getOutputStream());  
//发送参数  
out.print(param);  
//输出流的缓冲  
out.flush();  
//读取获取的数据  
in=new BufferedReader(new InputStreamReader(connection.getInputStream()));  
String line;  
while((line=in.readLine())!=null){  
result+=line;  
}   
}catch(Exception e){  
System.out.println("发送POST请求出现异常！" + e);  
e.printStackTrace();  
}finally {  
try{  
if(out!=null){  
out.close();  
}  
if(in!=null){  
in.close();  
}  
}catch (Exception e2) {  
System.out.println("关闭请求流出现异常！" + e2);  
e2.printStackTrace();  
}  
}  
return result;  
}  
  
}  